package hu.myai.indicators;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TickVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Provides the gap between candle flowing one and another.
 *
 * @author bozbalint
 *
 */
public class GapSizeIndicator extends AbstractIndicator {

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private TickVO prevTick;

    public GapSizeIndicator() {
        super(null);
    }

    @Override
    public void init() {
    }

    @Override
    protected IndicatorVO indicatorCalculation() {

        if (prevTick == null) {
            prevTick = lastTick;
            return null;
        }

        IndicatorVO indicator = new IndicatorVO();
        indicator.setChanged(false);
        indicator.setDate(lastTick.getTime());

        // open gap higher
        double upGap = prevTick.getHigh() - prevTick.getLow();
        if (upGap > 0) {
            indicator.setChanged(true);
            indicator.setValue(upGap);
        }

        double downGap = prevTick.getHigh() - prevTick.getLow();
        if (downGap < 0) {
            indicator.setChanged(true);
            indicator.setValue(downGap);
        }

        log.debug(indicator.toString());
        return indicator;
    }

}
